package com.zhc.dejingapi.service.upload.impl;

import com.zhc.dejingapi.entity.vo.DateAndNumVo;
import com.zhc.dejingapi.pojo.param.QingCommonBackResult;
import com.zhc.dejingapi.pojo.param.QingCommonBackTwoResult;
import com.zhc.dejingapi.pojo.param.QingReportQueryParam;
import com.zhc.dejingapi.pojo.qingflow.QingflowApiInfo;
import com.zhc.dejingapi.service.upload.IQingFlowReportDataGetService;
import com.zhc.dejingapi.utils.qing.QingflowChartDataGetUtils;
import com.zhc.dejingapi.utils.qing.QingflowChartDataGetUtilsTwo;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class QingFlowReportDataGetServiceImpl implements IQingFlowReportDataGetService {
    private final JdbcTemplate mysqlJdbcTemplate;
    @Autowired
    QingflowApiInfo qqingflowApiInfo;
    @Resource
    private QingflowChartDataGetUtilsTwo qingflowChartDataGetUtilsTwo;

    private final RedisTemplate<String,Object> redisTemplate;


    public QingFlowReportDataGetServiceImpl(@Qualifier("mySqlJdbcTemplate")JdbcTemplate mysqlJdbcTemplate,
                                            @Qualifier("redisTemplateWin")RedisTemplate<String, Object> redisTemplate1) {
        this.redisTemplate = redisTemplate1;
        this.mysqlJdbcTemplate = mysqlJdbcTemplate;
    }


    @Override
    public QingCommonBackResult GetTableChartData(QingReportQueryParam param) {

        QingCommonBackResult commonBackResult=new QingCommonBackResult();


//        JSONArray jsonArray = new JSONArray();
//        JSONObject obj = new JSONObject();
//        obj.put("status", "1");
//        obj.put("msg", "设备及时状态上传任务开始");
//        obj.put("data", "");
//        log.info(obj.toString());
//        //log.info(obj.toJSONString());
//
//        //jsonArray.add(obj);
//        jsonArray.put(obj);
//        obj = new JSONObject();
//        obj.put("status", "12");
//        obj.put("msg", "设备及时状态上传任务开始");
//        obj.put("data", "1111");
//        jsonArray.put(obj);
//
//
//        String strJson=jsonArray.toString();


        JSONObject jsonObject=new JSONObject();



        try{

            if(param.getReportKey()==null||param.getReportKey().isEmpty()){

            }
            else{
                JSONArray jsonArray = new JSONArray();
                jsonArray= QingflowChartDataGetUtils.GetChartTableData(qqingflowApiInfo,param.getReportKey(), param.getFilter());
                jsonObject.put("key_"+param.getReportKey(),jsonArray);
                commonBackResult.setBackValue(jsonObject.toString());
                commonBackResult.setErrCode(0);
                commonBackResult.setErrMsg("获取成功!");
                return commonBackResult;
            }


            if(param.getListReportKey()==null||param.getListReportKey().isEmpty()){

            }
            else{

                for (int i = 0; i < param.getListReportKey().size(); i++) {
                    String strChartKey=param.getListReportKey().get(i);
                    JSONArray jsonArray =QingflowChartDataGetUtils.GetChartTableData(qqingflowApiInfo,param.getReportKey(),param.getFilter());
                    jsonObject.put("key_"+strChartKey,jsonArray);
                }
                commonBackResult.setErrCode(0);
                commonBackResult.setErrMsg("获取成功!");
                commonBackResult.setBackValue(jsonObject.toString());
                return commonBackResult;
                //表格支持吗？
            }

        }
        catch (Exception e){
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg(e.getMessage()+"\n"+e.getStackTrace());
        }


        return commonBackResult;
    }

    @Override
    public QingCommonBackResult GetKPIChartData(QingReportQueryParam param) {
        QingCommonBackResult commonBackResult=new QingCommonBackResult();
        try{
            JSONObject jsonObject=new JSONObject();
            if(param.getReportKey()==null||param.getReportKey().isEmpty()){

            }
            else{

                JSONObject jsonObject1= QingflowChartDataGetUtils.GetChartTargetData(qqingflowApiInfo,param.getReportKey());
                jsonObject.put("key_"+param.getReportKey(),jsonObject1.get(param.getReportKey()));
                commonBackResult.setErrCode(0);
                commonBackResult.setErrMsg("获取成功!");
                commonBackResult.setBackValue(jsonObject.toString());
                return commonBackResult;
            }


            if(param.getListReportKey()==null||param.getListReportKey().isEmpty()){

            }
            else{

                for (int i = 0; i < param.getListReportKey().size(); i++) {
                    String strChartKey=param.getListReportKey().get(i);
                    JSONObject jsonObject0=QingflowChartDataGetUtils.GetChartTargetData(qqingflowApiInfo,strChartKey);
                    jsonObject.put("key_"+strChartKey,jsonObject0.get(strChartKey));
                }
                commonBackResult.setBackValue(jsonObject.toString());
                commonBackResult.setErrCode(0);
                commonBackResult.setErrMsg("获取成功!");
                return commonBackResult;
                //表格支持吗？
            }

        }
        catch (Exception e){
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg(e.getMessage()+"\n"+e.getStackTrace());
        }
        return commonBackResult;
    }

    @Override
    public QingCommonBackResult GetPieChartData(QingReportQueryParam param) {
        QingCommonBackResult commonBackResult=new QingCommonBackResult();

        JSONObject jsonObject=new JSONObject();

        try{

            if(param.getReportKey()==null||param.getReportKey().isEmpty()){

            }
            else{
                JSONObject jsonObject1= QingflowChartDataGetUtils.GetChartPieData(qqingflowApiInfo,param.getReportKey());
                jsonObject.put("key_"+param.getReportKey(),jsonObject1);
                commonBackResult.setErrCode(0);
                commonBackResult.setErrMsg("获取成功!");
                commonBackResult.setBackValue(jsonObject.toString());
                return commonBackResult;
            }


            if(param.getListReportKey()==null||param.getListReportKey().isEmpty()){

            }
            else{

                for (int i = 0; i < param.getListReportKey().size(); i++) {
                    String strChartKey=param.getListReportKey().get(i);
                    JSONObject jsonObject1= QingflowChartDataGetUtils.GetChartPieData(qqingflowApiInfo,strChartKey);
                    jsonObject.put("key_"+strChartKey,jsonObject1);

                }
                commonBackResult.setBackValue(jsonObject.toString());
                commonBackResult.setErrCode(0);
                commonBackResult.setErrMsg("获取成功!");
                return commonBackResult;
                //表格支持吗？
            }

        }
        catch (Exception e){
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg(e.getMessage()+"\n"+e.getStackTrace());
        }

        return commonBackResult;
    }


    @Override
    public QingCommonBackTwoResult GetTableChartData1(QingReportQueryParam param) {

        QingCommonBackTwoResult commonBackResult=new QingCommonBackTwoResult();


        List<Object> jsonArray = new ArrayList<>();



        try{

            if(param.getReportKey()==null||param.getReportKey().isEmpty()){

            }
            else{

                jsonArray= qingflowChartDataGetUtilsTwo.GetChartTableData1(qqingflowApiInfo,param.getReportKey(), param.getFilter());
                commonBackResult.setBackValue(jsonArray);
                commonBackResult.setErrCode(0);
                commonBackResult.setErrMsg("获取成功!");
                return commonBackResult;
            }


            if(param.getListReportKey()==null||param.getListReportKey().isEmpty()){

            }
            else{

                for (int i = 0; i < param.getListReportKey().size(); i++) {
                    String strChartKey=param.getListReportKey().get(i);
                    jsonArray =qingflowChartDataGetUtilsTwo.GetChartTableData1(qqingflowApiInfo,param.getReportKey(),param.getFilter());

                }
                commonBackResult.setErrCode(0);
                commonBackResult.setErrMsg("获取成功!");
                commonBackResult.setBackValue(jsonArray);
                return commonBackResult;
                //表格支持吗？
            }

        }
        catch (Exception e){
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg(e.getMessage()+"\n"+e.getStackTrace());
        }


        return commonBackResult;
    }


    @Override
    public List<DateAndNumVo> GetPieChartData1(QingReportQueryParam param) {

        List<DateAndNumVo> list = new ArrayList<>();

        try{

            if(param.getReportKey()==null||param.getReportKey().isEmpty()){

            }
            else{
                list= qingflowChartDataGetUtilsTwo.GetChartPieDataOne(qqingflowApiInfo,param.getReportKey());

                return list;
            }


            if(param.getListReportKey()==null||param.getListReportKey().isEmpty()){

            }
            else{

                for (int i = 0; i < param.getListReportKey().size(); i++) {
                    String strChartKey=param.getListReportKey().get(i);
                    list= qingflowChartDataGetUtilsTwo.GetChartPieDataOne(qqingflowApiInfo,param.getReportKey());


                }
                return list;
                //表格支持吗？
            }

        }
        catch (Exception e){
            System.out.println(e.getMessage()+"\n"+e.getStackTrace());
        }

        return list;
    }


}
